package com.hpsk.shopsatoken.model.user.service.impl;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.stp.parameter.SaLoginParameter;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hpsk.common.CommonException;
import com.hpsk.common.CommonPageRequest;
import com.hpsk.common.PageCommon;
import com.hpsk.shopsatoken.model.user.dao.UserMapper;
import com.hpsk.shopsatoken.model.user.dto.LoginParamDto;
import com.hpsk.shopsatoken.model.user.dto.UserDto;
import com.hpsk.shopsatoken.model.user.entity.User;
import com.hpsk.shopsatoken.model.user.service.IUserService;
import com.hpsk.shopsatoken.model.user.vo.UserVo;
import io.github.linpeilie.Converter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author huangwang
 * @since 2025-05-22
 */
@Service
@Slf4j
@AllArgsConstructor(onConstructor_ = @__(@Autowired))
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    //token有效期（7天）
    private final static long TOKEN_TIME = 60 * 60 * 24 * 7;
    private final Converter converter;

    @Override
    public SaTokenInfo login(LoginParamDto dto) {
        String phone = dto.getPhone();
        String password = dto.getPassword();
        User user = getOne(Wrappers.<User>lambdaQuery().eq(User::getPhone, phone));
        if (user == null) {
            throw new CommonException("用户不存在！");
        }
        if (!user.getPassword().equals(password)) {
            throw new CommonException("密码错误！");
        }
//        StpUtil.logout(user.getId());
        StpUtil.login(user.getId(), new SaLoginParameter()
                // 是否为持久Cookie（临时Cookie在浏览器关闭时会自动删除，持久Cookie在重新打开后依然存在）
                .setIsLastingCookie(true)
                .setTimeout(TOKEN_TIME)
                .setExtra("name", user.getName()));
        return StpUtil.getTokenInfo();
    }

    @Override
    public Page<User> selectPageVo(PageCommon pageCommon) {
        IPage<User> page = page(CommonPageRequest.defaultPage());
        return (Page<User>) page;
    }

    @Override
    public User add(UserDto userDto) {
        User user = BeanUtil.copyProperties(userDto, User.class);
        save(user);
        return user;
    }

    @Override
    public User deleteById(Long id) {
        User user = getById(id);
        if (user != null) {
            removeById(id);
            return user;
        }
        throw new CommonException(String.format("%s删除失败！", id));
    }

    @Override
    public UserVo detail(Long id) {
        User user = getById(id);
        return converter.convert(user, UserVo.class);
    }
}
